package cn.wolfcode.wms.controllrer;

import cn.wolfcode.wms.domain.Department;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmetController {

    @Autowired
    private IDepartmentService service ;


    @RequestMapping("/list")
    @RequiredPermission("部门列表")
    public String list(Model model ,QueryObject qo){

        PageResult pageResult = service.query(qo);

        model.addAttribute("pageResult",pageResult);
        return "department/list";
    }
    @RequiredPermission("部门删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        try {
            service.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }

        return  JSONResult.success("删除成功");
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id != null){
            model.addAttribute("department",service.selectByPrimaryKey(id));
        }
        return "department/input";

    }


    @RequiredPermission("部门添加或删除")
    @RequestMapping("/saveorupdate")
    public String  saveorupdate(Department department){

        System.out.println(department.getName()+"11111111111111111111111111");
        System.out.println(department.getSn()+"11111111111111111111111111");
        try {
            service.saveorupdate(department);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/department/list.do";
    }

}
